import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.*;
import java.net.*;

public class Exp {
    public static void main(String args[]) {
        File src = new File("src");
        File[] filesIn_src = src.listFiles();
        for (File fileIn_src : filesIn_src) {
            if (fileIn_src.isDirectory()) {
                File pkg = fileIn_src;
                String packageName = pkg.getName();
                File[] filesInPackage = pkg.listFiles();
                for (File fileInPackage : filesInPackage) {
                    //确保是Java源文件
                    String fileName = fileInPackage.getName();
                    if (fileInPackage.isFile()
                            && fileName.substring(fileInPackage.getName().length() - 5,
                            fileName.length()).equals(".java")) {
                        File srcFile = fileInPackage;
                        addPkgDeclaration(srcFile, packageName);
                    }
                }
            }
        }
    }

    private static void addPkgDeclaration(File src, String pkgName) {
        //调用者保证文件存在
        ArrayList<String> lines = new ArrayList<>();
        lines.add("package " + pkgName + ";");
        try (Scanner in = new Scanner(src)) {
            while (in.hasNext()) {
                lines.add(in.nextLine());
            }
        }
        catch (IOException ex) {
            ex.printStackTrace();
        }
        try (PrintWriter out = new PrintWriter(src)) {
            for (String line : lines) {
                out.println(line);
            }
        }
        catch (IOException ex) {
            ex.printStackTrace();
        }
    }

}